<template>
	<view class="app_Cont">
		<view class="showCont" v-if="showCont">
			<view class="accountCont">
				<view class="accountItem" v-if="item.type==2" :key="index" v-for="(item,index) in listArr">
					<view class="accountName">
						<view class="accountNameInfo">
							<view>{{item.name}}</view>
							<view class="accountTag" v-if="item.status==1">正常</view>
							<view class="accountTag" v-if="item.status==2">禁用</view>
						</view>
						<view class="accountEdit iconfont icon-gengduo" @click="ctrlFn(item)"></view>
					</view>
					<view class="accountNum">{{item.mobile}}</view>
					<view class="creatTime">创建时间：{{item.create_time}}</view>
				</view>
				<view class="noTip" v-if="listArr.length==0">本学校还没有子账号~</view>
				<view class="addAccount" @click="addFn" v-if="listArr.length<=6">+新增子账号</view>
			</view>
		</view>
		<u-popup :show="showCtrl" @close="showCtrl = false" :closeOnClickOverlay="true" :safeAreaInsetBottom="false" mode="bottom" :round="10">
			<view class="ctrlGroup">
				<view class="ctrlItem hasBottom" v-if="activeItem.status==1" @click="bindFn">禁用账号</view>
				<view class="ctrlItem hasBottom" v-if="activeItem.status==2" @click="resetFn">恢复账号</view>
				<view class="ctrlItem hasBottom" @click="editFn">编辑账号</view>
				<view class="ctrlItem" @click="deleteFn">删除账号</view>
			</view>
		</u-popup>
		<u-popup :show="showAdd" @close="showAdd = false" :closeOnClickOverlay="true" :safeAreaInsetBottom="false" mode="bottom" :round="10">
			<view class="formCont">
				<view class="formHead">{{activeId?'编辑':'新增'}}子账号</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">负责人</view>
				    </view>
					<view class="inputValue">
						<input maxlength="10" v-model="activeItem.name" placeholder="请输入对您的称呼"  />
					</view>
				</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">手机号</view>
				    </view>
					<view class="inputValue">
						<input maxlength="11" type="number" v-model="activeItem.mobile" placeholder="请输入手机号"  />
					</view>
					<view v-if="checkPhone(activeItem.mobile)" class="iconfont icon-gouxuan1"></view>
				</view>
				<view class="sureBtn" @click="sureReport">{{activeId?'保存设置':'确认新增'}}</view>
			</view>
		</u-popup>
		<u-notify ref="uNotify"></u-notify>
		<u-modal :show="showModal" :showCancelButton="true" @cancel="showModal = false" title="绑定提示"  @confirm="modalSure"  :content='modalCont'></u-modal>
		<u-toast ref="uToast"></u-toast>
		<subLoad v-if="showSub"></subLoad>
		<load v-if="!showCont"></load>
	</view>
</template>
<script>
	import ruleCheck from '@/utils/ruleCheck.js'
	import subLoad from '@/components/sub.vue'
	import api from '@/http/api.js'
	import load from '@/components/load.vue'
	export default {
		components:{load,subLoad},
		data() {
			return {
				showCont:false,
				showSub:false,
				showAdd:false,
				showCtrl:false,
				showModal:false,
				modalCont:'',
				listArr:[],
				activeItem:{
					name:'',
					mobile:'',
				},
				activeId:'',
				ctrlType:'',
			};
		},
		methods:{
			modalSure(){
				let data=  {
					school_admin_id:this.activeItem.school_admin_id
				}
				this.showSub = true
				if(this.ctrlType=='delete'){
					api.getRequst(data,'/api/School/accountDel').then((res)=>{
						if(res.code==1){
							this.$refs.uToast.show({
								message:'删除成功',
							})
							this.getData()
							this.showModal =false
						}
						this.showSub = false
					})
				}else if(this.ctrlType=='bind'||this.ctrlType=='reset'){
					data['status'] = this.ctrlType=='reset'?1:2
					api.getRequst(data,'/api/School/accountCheckStatus').then((res)=>{
						if(res.code==1){
							this.$refs.uToast.show({
								message:'操作成功',
							})
							this.getData()
							this.showModal =false
						}
						this.showSub = false
					})
				}
			},
			ctrlFn(item){
				this.activeItem = item
				this.activeId = item.school_admin_id
				this.showCtrl = true
			},
			sureReport(){
				let tipData = {
					bgColor: '#e47470',
					message: '',
				}
				if(this.activeItem.name.length<2){
					tipData.message = '负责人不能低于2个字'
					this.$refs.uNotify.show(tipData)
					return false
				}else if(ruleCheck.checkPhone(this.activeItem.mobile)){
					tipData.message = ruleCheck.checkPhone(this.activeItem.mobile)
					this.$refs.uNotify.show(tipData)
					return false
				}
				if(this.activeId){
					let data = {
						name:this.activeItem.name,
						mobile:this.activeItem.mobile,
						school_admin_id:this.activeItem.school_admin_id,
					}
					api.getRequst(data,'/api/School/accountEdit').then((res)=>{
						if(res.code==1){
							this.$refs.uToast.show({
								message:'编辑成功',
							})
							this.getData()
							this.showAdd =false
						}
					})
				}else{
					api.getRequst(this.activeItem,'/api/School/accountAdd').then((res)=>{
						if(res.code==1){
							this.$refs.uToast.show({
								message:'添加成功',
							})
							this.getData()
							this.showAdd =false
						}
					})
				}
			},
			checkPhone(){
				return !ruleCheck.checkPhone(this.activeItem.mobile)
			},
			bindFn(){
				this.ctrlType = 'bind'
				this.showModal = true
				this.modalCont = '确定禁用此账号吗？禁用后此账号将无法进行本学校相关操作。'
				this.showCtrl = false
			},
			resetFn(){
				this.ctrlType = 'reset'
				this.showModal = true
				this.modalCont = '确定恢复此账号吗？恢复后此账号将正常使用。'
				this.showCtrl = false
			},
			deleteFn(){
				this.ctrlType = 'delete'
				this.showModal = true
				this.modalCont = '确定删除此账号吗？删除之后此账号将不会再关联本学校。'
				this.showCtrl = false
			},
			editFn(){
				this.showAdd = true
				this.showCtrl = false
			},
			addFn(){
				this.activeItem = {
					name:'',
					mobile:'',
				}
				this.activeId=  ''
				this.showAdd = true
			},
			getData(){
				let data = {
					page:1,
					per_page:10,
				}
				api.getRequst(data,'/api/School/account').then((res)=>{
					if(res.code==1){
						this.listArr = res.data.data
					}
					this.showCont = true
				})
			}
		},
		onLoad() {
			this.getData()
		}
	}
</script>

<style lang="less">
.showCont{
	min-height: 100vh;
	padding-bottom:30rpx;
	box-sizing: border-box;
	padding-top: 30rpx;
	.accountCont{
		width: 92%;
		margin: 0 auto;
		.accountItem{
			padding: 30rpx;
			box-sizing: border-box;
			background-color: #fff;
			margin-bottom: 30rpx;
			border-radius: 20rpx;
			.accountName{
				display: flex;
				justify-content: space-between;
				place-items: center;
				.accountNameInfo{
					display: flex;
					align-items: center;
					font-size: 30rpx;
					font-weight: bold;
					.accountTag{
						font-size: 20rpx;
						font-weight: normal;
						margin-left: 10rpx;
						background-color: #3f55cc;
						color: #fff;
						padding: 5rpx 20rpx;
						border-radius: 5rpx;
					}
				}
			}
			.accountNum{
				font-size: 30rpx;
				margin-top: 15rpx;
			}
			.creatTime{
				color: #666;
				font-size: 24rpx;
				margin-top: 10rpx;
			}
		}
		.noTip{
			color: #999;
			font-size: 24rpx;
			text-align: center;
			padding-top: 30rpx;
			padding-bottom: 50rpx;
		}
		.addAccount{
			width: 100%;
			background-image: linear-gradient(to right bottom,#a3bffe,#3f55cc);
			color: #fff;
			text-align: center;
			padding: 24rpx 0;
			border-radius: 100rpx;
			font-size: 24rpx;
		}
	}
}
.ctrlGroup{
	.ctrlItem{
		display: flex;
		justify-content: center;
		padding: 30rpx 0;
		letter-spacing: 2rpx;
	}
	.hasBottom{
		border-bottom: 1px solid #f6f7f9;
	}
}
.formCont{
	padding: 30rpx 4%;
	width: 100%;
	box-sizing: border-box;
	.formHead{
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
	}
	.formItem{
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		background-color: #f8f8f8;
		padding: 25rpx 50rpx;
		box-sizing: border-box;
		border-radius: 50rpx;
		.inputName {
		  width: 140rpx;
		  position: relative;
		  font-size: 28rpx;
		  .mustIcon {
			font-size: 28rpx;
			top: 0;
			left: -16rpx;
			position: absolute;
			color: #ff5757;
		  }
		}
		.inputOut {
			flex: 1;
			width: 3rem;
			position: relative;
			.inputMask {
				width: 100%;
				height: 100%;
				position: absolute;
				left: 0;
				top: 0;
				z-index: 2;
			  }
			  input{
				width: 100%;
				border: none;
				background: none;
				outline: none;
				position: relative;
				z-index: 1;
			  }
		}
		.inputValue{
			flex: 1;
			width: 100rpx;
		}
		.icon-gouxuan1,.icon-xiala{
			color: #3f55cc;
		}
	}
	.sureBtn{
		width: 100%;
		padding: 25rpx 0;
		text-align: center;
		background-image: linear-gradient(to right bottom,#a3bffe,#3f55cc);
		color: #fff;
		font-size: 24rpx;
		border-radius: 100rpx;
		margin-top: 40rpx;
	}
}
</style>
